'use strict';
app.controller('questionTypeCtrl',['$scope','utilService','toaster','questionTypeService','$modal', function($scope,utilService,toaster,questionTypeService,$modal){
    $scope.questionType={
        name:''
    }
    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'question/type',
            params:{name: $scope.questionType.name}
        }).then(function(result){
        });
    }
    $scope.query();

    //打开添加框
    $scope.openAdd = function () {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/question/question_type_add.html',
            controller: 'questionTypeAddCtrl',
            size: 'sm',
            scope:$scope,
        });
    };


    $scope.openEdit = function (id,name) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/question/question_type_edit.html',
            controller: 'questionTypeEditCtrl',
            size: 'sm',
            scope:$scope,
            resolve: {
                data:function(){
                    var data={};
                    data.id=id;
                    data.name=name;
                    return data;
                }
            }
        });
    };

    //置顶
    $scope.toTop= function(id){
        $scope.questionType.id=id;
        questionTypeService.toTop($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query();
                    break;
                case 684:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    $scope.openDelete = function (id,name) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/question/question_type_delete.html',
            controller: 'questionTypeDeleteCtrl',
            scope:$scope,
            size: 'sm',
            resolve: {
                data:function(){
                    var data={};
                    data.id=id;
                    data.name=name;
                    return data;
                }
            }
        });
    };

}]);

app.controller('questionTypeAddCtrl', ['$scope','$state','$modalInstance','questionTypeService','toaster',
    function($scope,$state,$modalInstance,questionTypeService,toaster) {
        $scope.questionType={
            name:''
        };
        $scope.AllowToAdd=true;
        $scope.add=function(){
            if(!$scope.AllowToAdd){
                return;
            }
            $scope.AllowToAdd=false;
            questionTypeService.add($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.cancel();
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToAdd=true;
                        $state.reload('app.question');
                        break;
                    case 684:
                    case 685:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToAdd=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToAdd=true;
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);


app.controller('questionTypeEditCtrl', ['$scope', '$modalInstance','$state','questionTypeService','toaster','data',
    function($scope, $modalInstance,$state,questionTypeService,toaster,data) {
        $scope.data=data;
        $scope.AllowToEdit=true;
        $scope.edit =function(){
            if(!$scope.AllowToEdit){
                return;
            }
            $scope.AllowToEdit=false;

            questionTypeService.update($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.cancel();
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToEdit=true;
                        $state.reload('app.question');
                        break;
                    case 684:
                    case 685:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToEdit=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToEdit=true;
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

    }]);

//删除模态框Controller
app.controller('questionTypeDeleteCtrl', ['$scope', '$modalInstance','$state','questionTypeService','toaster','data',
    function($scope, $modalInstance,$state,questionTypeService,toaster,data) {
        $scope.data=data;
        $scope.AllowToDelete=true;

        //确认删除
        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }
            $scope.AllowToDelete=false;

            var id=$scope.data.id;
            questionTypeService.delete($scope,id).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToDelete=true;
                        $state.reload('app.question');
                        break;
                    case 687:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToDelete=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToDelete=true;
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);